package com.qf.efserver.device.service.impl;

import com.qf.common.core.entity.DeviceConnections;
import com.qf.common.core.utils.TokenUtils;
import com.qf.efserver.device.VO.DeviceVO;
import com.qf.efserver.device.mapper.DeviceMapper;
import com.qf.efserver.device.param.AddDeviceParam;
import com.qf.efserver.device.param.UpdateDeviceParam;
import com.qf.efserver.device.service.DeviceService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * --- 遇事不决，可问春风。春风不语，遵循己心。---
 *
 * @date 2024/11/13
 */
@Service
public class DeviceServiceImpl implements DeviceService {
    @Resource
    private DeviceMapper deviceMapper;

    /**
     * 展示用户的设备
     *
     * @return deviceVO
     */
    @Override
    public List<DeviceVO> selectDeviceByUserId() {
        //取用户的id
        Integer userId = TokenUtils.getUidFromToken();
        //通用户的id去查询用户的设备
        List<DeviceVO> deviceVOS = deviceMapper.selectDeviceByUserId(userId);
        return deviceVOS;
    }

    /**
     * 增加用户的设备
     *
     * @param addDeviceParam
     */
    @Override
    public void addDevice(AddDeviceParam addDeviceParam) {
        //取用户的id
        Integer userId = TokenUtils.getUidFromToken();
        //查询用户的设备
        List<DeviceVO> deviceVOS = deviceMapper.selectDeviceByUserId(userId);
        //设备的数量
        int size = deviceVOS.size();
        //判断用户数量
        if (size >= 5) {
            throw new RuntimeException("设备数不能超过五个");
        }
        DeviceConnections deviceConnections = new DeviceConnections();
        BeanUtils.copyProperties(addDeviceParam,deviceConnections);
        deviceConnections.setUserId(userId);
        deviceMapper.insert(deviceConnections);
    }

    /**
     * 修改用户的设备状态
     *
     * @param updateDeviceParam
     */
    @Override
    public void updateDeviceById(UpdateDeviceParam updateDeviceParam) {
        //取用户的id
        Integer userId = TokenUtils.getUidFromToken();
        //从前端传过来的数据取出来ConnectionStatus字段并强转
        Integer parseInt = Integer.parseInt(updateDeviceParam.getConnectionStatus());
        //如果parseInt不为空且为一
        if (parseInt == 1 && parseInt != null) {
            //从前端传来的数据里面取id并直接修改数据
            deviceMapper.updateDeviceId(updateDeviceParam.getDeviceId());
        }else if (parseInt == 2 && parseInt != null) {
            //将用户id的数据connectionStatus字段全部修改为2、未连接
            deviceMapper.updateDeviceByUid(userId);
            //查询用户的数据
            List<DeviceVO> updateDeviceParams = deviceMapper.selectDeviceByUserId(userId);
            List<Integer> params = new ArrayList<>();
            //遍历、取出需要的字段强转为Integer类型并添加到list集合中
            for (DeviceVO deviceParam : updateDeviceParams) {
                params.add(Integer.parseInt(deviceParam.getConnectionStatus()));
            }
            //用stream流判断list集合中的数据是否等于1、并返回一个布尔类型
            boolean isConnected = params.stream()
                    .allMatch(status -> status == 1);
            //不等于一则通过deviceId修改数据
            if (!isConnected) {
                Integer deviceId = updateDeviceParam.getDeviceId();
                deviceMapper.updateDeviceById(deviceId);
            } else {
                //等一则报错
                throw new RuntimeException("连接异常");
            }

        }else {
            deviceMapper.updateDeviceByIds(updateDeviceParam.getDeviceId());
        }
    }

    @Override
    public void deleteDeviceById(Integer id) {
        deviceMapper.deleteDeviceById(id);
    }
}
